package com.neu.dao.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
import com.neu.dao.IworkerDao;
import com.neu.pojo.Worker;

public class WorkerDao implements IworkerDao {
	//����	
	private File file = new File("worker.json");
	private ObjectMapper om = new ObjectMapper();
	//����ģʽ
	private static IworkerDao workerDao = new WorkerDao();
	private WorkerDao() {}
	public static IworkerDao getInstance() {
		return workerDao;
	}
	
	
	//����ע��
	@Override
	public boolean add(Worker worker) {
		ArrayList<Worker> workers = new ArrayList<>();
		
		try {
			workers = this.om.readValue(file,new TypeReference<List<Worker>>(){});//�ļ���Ϊ��
			for(Worker w:workers) {
				if(w.getNumber().equals(worker.getNumber())) {
					return false;
				}
			}
			workers.add(worker);//��ӵ�����
			this.om.writeValue(file, workers);//д�뵽�ļ�
			return true;//������
		} catch (MismatchedInputException e) {//�ļ�Ϊ�գ���һ����ӹ�����Ա
			workers.add(worker);//���Ϊ�����е�һ��Ԫ��
			try {
				this.om.writeValue(file, workers);//д��
				return true;
			} catch (IOException e1) {
				e1.printStackTrace();
				return false;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}

	//���ڲ�����Ϣ,��¼��
	@Override
	public Worker search(Worker worker) {
		ArrayList<Worker> workers = new ArrayList<>();
		
		try {
			workers = this.om.readValue(file,new TypeReference<List<Worker>>(){});//�ļ���Ϊ��

			for(Worker w:workers) {
				if(w.getNumber().equals(worker.getNumber())) {
					return w;
				}
			}
			System.out.println("1");
			return null;
		} catch (MismatchedInputException e) {//�ļ�Ϊ�գ���һ����ӹ�����Ա
			return null;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//�����޸Ĺ�����Ա��Ϣ
	@Override
	public boolean cover(Worker worker) {
		ArrayList<Worker> workers = new ArrayList<>();
		
		try {
			workers = this.om.readValue(file,new TypeReference<List<Worker>>(){});//�ļ���Ϊ��
			for(int i=0;i<workers.size();i++) {
				if(workers.get(i).getNumber().equals(worker.getNumber())) {
					workers.get(i).setBirthday(worker.getBirthday());
					workers.get(i).setJob(worker.getJob());
					workers.get(i).setKind(worker.getKind());
					workers.get(i).setName(worker.getName());
					workers.get(i).setPassword(worker.getPassword());
					workers.get(i).setPhone(worker.getPhone());
					this.om.writeValue(file, workers);
					return true;
				}
			}
			return false;
		} catch (MismatchedInputException e) {//�ļ�Ϊ��,������
			return false;
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	//�õ�ȫ������
	@Override
	public ArrayList<Worker> list(){
		ArrayList<Worker> workers = new ArrayList<>();//�ļ���Ϊ��
		
		try {
			workers = this.om.readValue(file,new TypeReference<List<Worker>>(){});
			return workers;
		}catch (MismatchedInputException e) {
			return workers;
		}catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	//删除
	@Override
	public boolean delete(Worker w) {
		ArrayList<Worker> workers = new ArrayList<>();
		
		try {
			workers = this.om.readValue(file,new TypeReference<List<Worker>>(){});
			for(int i=0;i<workers.size();i++) {
				if(workers.get(i).getNumber().equals(w.getNumber())) {
					workers.remove(i);
					om.writeValue(file, workers);
					return true;
				}
			}
			return false;
		}catch (MismatchedInputException e) {
			return false;
		}catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	
	public static void main(String[] args) {
//		Worker w = new Worker("1", "1", "kwc", "1", "1", "1", "1");
//		Worker w1 = new Worker("2", "2", "sun", "2", "2", "2", "2");
//		WorkerDao.getInstance().add(w);
//		WorkerDao.getInstance().add(w1);
	}
}
